﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using DataABCDModel;

/// <summary>
/// Summary description for Connect
/// </summary>
public class Connect
{
    public SqlConnection conn = new SqlConnection("server=ZER0;database=DataABCD;uid=sa;pwd=lam123456");
    public SqlDataReader reader = null;
    public SqlCommand comm = null;
    string commStr = string.Empty;
    SqlDataAdapter adapter = null;
    DataSet ds = null;
    DataView dt = null;
    DataViewManager dvm = null;

    
	public Connect()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int Total(int int_c1,DateTime dt_start,DateTime dt_end,string str_con)
    {
        int int_rs = 0;
        try
        {
            conn.Open();
            comm = new SqlCommand(str_con, conn);
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                if(reader.GetDateTime(int_c1)>=dt_start)
                {
                    if (reader.GetDateTime(int_c1)<= dt_end)
                    {
                        int_rs++;
                    }
                }
            }
            reader.Close();
            conn.Close();
        }
        catch(Exception)
        {
        }
        return int_rs;
    }
    public float TotalM(int int_c1,int int_c2, DateTime dt_start, DateTime dt_end, string str_con)
    {
        float float_rs = 0;
        try
        {
            conn.Open();
            comm = new SqlCommand(str_con, conn);
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                if (reader.GetDateTime(int_c1) >= dt_start)
                {
                    if (reader.GetDateTime(int_c1) <= dt_end)
                    {
                        float_rs = float.Parse(reader.GetValue(int_c2).ToString()) + float_rs;
                    }
                }
            }
            reader.Close();
            conn.Close();
        }
        catch (Exception)
        {
        }
        return float_rs;
    }
    public DataSet LoadData(string str_table, string str_con)
    {
        DataSet data = new DataSet();
        try
        {
            conn.Open();
            adapter = new SqlDataAdapter(str_con, conn);
            adapter.Fill(data, "" + str_table + "");
            conn.Close();
        }
        catch (Exception)
        {
            conn.Close();
        }
        return data;
    }
    public string GetNameM(int int_c,string str_con)
    {
        string str_rs = null;
        try
        {
            conn.Open();
            comm = new SqlCommand(str_con, conn);
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                str_rs = reader.GetString(int_c);
            }
            reader.Close();
            conn.Close();
        }
        catch (Exception)
        {
        }
        return str_rs;
    }
    public bool ADD_Edit(string str)
    {
        try
        {
            conn.Open();
            comm = new SqlCommand(str, conn);
            if (comm.ExecuteNonQuery() > 0)
            {
                return true;
            }
            conn.Close();
        }

        catch (Exception)
        {
            conn.Close();
        }
        return false;
    }
    public string GetID(string table )
    {
        string str_id=null;
        string str_rs=null;
        try
        {
            conn.Open();
            string str_con = "SELECT COUTH * FROM " + table + "";
            comm = new SqlCommand(str_con, conn);
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                str_id = reader.GetString(0);
            }
            reader.Close();
            conn.Close();
        }
        catch (Exception)
        {
        }
        return str_id;
    }
}